package com.fourWork.workFive;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client extends Thread {
    //声明Socket对象
    Socket socket;
    //声明BufferedReader对象
    BufferedReader bufferedReader = null;
    //声明PrintStream对象
    PrintStream printStream = null;
    Scanner scanner = new Scanner(System.in);

    public Client(Socket socket) {
        this.socket = socket;
    }

    public static void main(String[] args) {

        try {
            Socket socket=new Socket("127.0.0.1", 8888);
            Thread thread=new Sent(socket);
            Thread thread1=new Receive(socket);
            thread.start();
            thread1.start();
            thread.join();
            thread1.join();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
/*
    public void send(String str, int port) {
        try {
            socket = new Socket(str, port);    //创建Socket类型的对象并提供服务器的主机名和端口号
            System.out.println("连接服务器成功！");

            //使用输入输出流进行通信
            printStream = new PrintStream(socket.getOutputStream());

            while (true) {
                System.out.println("请输入要发送的数据内容：");
                String next = scanner.next();
                printStream.println(next);  //客户端向服务器发送字符串内容
                System.out.println("客户端发送数据内容成功");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printStream != null) {
                printStream.close();
            }
        }
    }
    */
 /*
    public void receive() {
        //接受服务器发来的字符串内容
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true) {
                String s = bufferedReader.readLine();//接受服务器发来的字符串内容并打印
                System.out.println("服务器回发的消息为：" +s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  */

}


class Sent extends Thread {

    //声明Socket对象
    Socket socket;
    //声明PrintStream对象
    PrintStream printStream = null;
    Scanner scanner = new Scanner(System.in);
    String str;
    int port;

    public Sent(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            //socket = new Socket(str, port);    //创建Socket类型的对象并提供服务器的主机名和端口号
            //socket = socket;    //创建Socket类型的对象并提供服务器的主机名和端口号
            System.out.println("连接服务器成功！");

            //使用输入输出流进行通信
            printStream = new PrintStream(socket.getOutputStream());

            while (true) {
                System.out.println("请输入要发送的数据内容：");
                String next = scanner.next();
                printStream.println(next);  //客户端向服务器发送字符串内容
                System.out.println("客户端发送数据内容成功");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printStream != null) {
                printStream.close();
            }
        }

    }
}

class Receive extends Thread {
    //声明Socket对象
    Socket socket;
    //声明BufferedReader对象
    BufferedReader bufferedReader = null;
    public Receive(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

            //接受服务器发来的字符串内容
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true) {
                    String s = bufferedReader.readLine();//接受服务器发来的字符串内容并打印
                    System.out.println("服务器回发的消息为：" + s + ",该数据由" + socket.getInetAddress() + "客户端所发");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

}
